﻿
using System;
using System.ServiceModel;

using WSU.Cheka.HelloWCF.Contracts;
using WSU.Cheka.HelloWCF.Services;

namespace WSU.Cheka.HelloWCF.Host
{
    /// <summary>
    /// bind the same service to different endpoints using different transmission protocol
    /// </summary>
    static class MultiEndpointHost
    {
        private static void HostViaCode()
        {
            Uri tcpBaseAddress = new Uri("net.tcp://localhost:9000/wsu");
            Uri pipeBaseAddress = new Uri("net.pipe://localhost/wsu");

            using (ServiceHost host = new ServiceHost(typeof(WsuService), tcpBaseAddress, pipeBaseAddress))
            {
                // no child is given, so the service address is just the same as the base address
                // given the binding, it will find the proper base address by matching the binding
                host.AddServiceEndpoint(typeof(IWsuContract), new NetTcpBinding(), string.Empty);
                host.AddServiceEndpoint(typeof(IWsuContract), new NetNamedPipeBinding(), string.Empty);

                host.Opened += (sender, evtargs) =>
                    {
                        Console.WriteLine("[Manual] Two endpoints opened, one for TCP, another for NamedPipe.");
                    };
                host.Open();
                Console.ReadLine();
            }
        }

        private static void HostViaConfig()
        {
            using (ServiceHost host = new ServiceHost(typeof(WsuService)))
            {
                host.Opened += (sender, evtargs) =>
                {
                    Console.WriteLine("[Config] Two endpoints opened, one for TCP, another for NamedPipe.");
                };
                host.Open();
                Console.ReadLine();
            }
        }

        public static void TestMain()
        {
            // HostViaCode();
            HostViaConfig();
        }
    }
}